/**
 * Created by gongxun on 16/11/21.
 */

var KwxTableMsgStatus = function(){
    var that = {};

    that.useTing = false;

    that.useTingKouNums = [];

    that.useKouTing = false;

    that.stopUseTing = function(){
        that.useTing = false;
        that.useTingKouNums = [];
    };

    that.sendPlayCard = function(cardNum){
        if (that.getMyIsPlayCard()){
            var cardNums = [cardNum];
            if (that.useTing){
                that.sendLiangTingCard(cardNums, that.useTingKouNums);
            }else{
                var isSend = mo.gameMsgManager.sendActionRequestkwx(7, cardNums, []);
                if (isSend){
                    //启动预先出牌
                    that.onPredictionCard(cardNum);
                }
            }
        }
        // if (that.mTableStatus.IsPlayCard){
        //
        //     var NowSeat = that.mTableStatus.NowSeat;
        //     if (NowSeat.length == 0){
        //     }else if (NowSeat.length == 1){
        //         var seat = that.getViewForLogic(NowSeat[0] - 1, that.mTableStatus.MySeat - 1);
        //         if (seat == 0){
        //             var cardNums = [cardNum];
        //             if (that.useTing){
        //                 that.sendLiangTingCard(cardNums, that.useTingKouNums);
        //             }else{
        //                 mo.gameMsgManager.sendActionRequestkwx(7, cardNums, []);
        //             }
        //         }
        //     }else{
        //     }
        // }
    };

    that.sendOpenCard = function(actionRequestkwx){
        mo.gameMsgManager.sendActionRequestkwx(actionRequestkwx.code, actionRequestkwx.Cards1, []);
    };

    //cardNum 用户打出的废牌    kouCards 用户扣的牌 去掉重复22 22 22 为22
    that.sendLiangTingCard = function (cardNums, kouCards) {
        mo.gameMsgManager.sendActionRequestkwx(0, cardNums, kouCards);

        cc.log("扣牌:" + kouCards.toString() + "扣牌长度是:" + kouCards.length);
    };

    //返回自己是否可以出牌
    that.getMyIsPlayCard = function(){
        if (that.mTableStatus.IsPlayCard){
            var seatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            for (var i = 0; i < seatUser.NewActionList.length; i++){
                if (seatUser.NewActionList[i].code == 7){
                    return true;
                }
            }
        }

        return false;
    };

    //预先出牌
    that.onPredictionCard = function(cardNum){
        cc.log("预先出牌");
        that.playDealCard(cardNum);

        that.mTableStatus.IsPlayCard = false;
    };

    //预先出牌验证
    that.checkPredictionCard = function(LastAction, SeatUser){
        cc.log("检测预先出牌");
        var hasError = false;
        var playerModel = mo.kwxTable.mKwxCardLayer.mPlayers[0];

        //检测手牌一致性
        var uiCardNums = [];
        var svCardNums = SeatUser.HandCards.slice(0);
        for (var i = 0; i < playerModel.inhandCards.length; i++){
            uiCardNums.push(playerModel.inhandCards[i].mCardNum);
        }
        uiCardNums.sort();
        svCardNums.sort();

        if (!mo.gameHelper.cmpareArray(uiCardNums, svCardNums)){
            hasError = true;
        }

        //检测打出去牌一致性
        uiCardNums = [];
        svCardNums = SeatUser.PlayCards.slice(0);
        for (var i = 0; i < playerModel.dealCards.length; i++){
            uiCardNums.push(playerModel.dealCards[i].mCardNum);
        }
        if (!mo.gameHelper.cmpareArray(uiCardNums, svCardNums)){
            hasError = true;
        }

        if (hasError){
            cc.log("预处理错误，正在还原");
            that.handlePredictionCardError(LastAction, SeatUser);
        }else{
            //没有问题不用处理
        }
    };

    //预先出牌错误处理
    that.handlePredictionCardError = function(LastAction, SeatUser){
        var playerModel = mo.kwxTable.mKwxCardLayer.mPlayers[0];

        if ((playerModel.inhandCards.length - 1 == SeatUser.HandCards.length) &&
            (playerModel.dealCards.length + 1 == SeatUser.PlayCards.length)){
            //自动出牌问题
            that.playDealCard(LastAction.Cards1[0]);
        }else{
            cc.log("检测到不一致了 开始重绘手牌了");
            mo.kwxTable.resetTable();
            mo.kwxTable.mKwxCardLayer.setReconnectStatus(that.mTableStatus);
        }
    };

    //打出手牌
    that.playDealCard = function(cardNum){
        var LastActionSeat = 0;
        var dealCard = cardNum;

        //其他玩家正常出牌
        mo.kwxTable.setDealCard(LastActionSeat, dealCard);

        //提示玩家打出的牌
        var showDealCard = [0, 0, 0];
        showDealCard[LastActionSeat] = dealCard;
        mo.kwxTable.mKwxTableUILayer.mKwxTableResultLayer.showDealCard(showDealCard);
    };

    return that;
};